package com.qfedu.edu.utils;

import com.alibaba.fastjson.JSON;
import com.qfedu.edu.result.R;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
public class HttpResponseUtils {
    public static void sendResponse(HttpServletResponse response, R r){

        String jsonStr = JSON.toJSONString(r);
        PrintWriter writer = null;
        response.setContentType("text/html;charset=utf-8");
        try {
            writer = response.getWriter();
            writer.write(jsonStr);
        } catch (IOException e) {
            log.info("响应出错：{}",e.getMessage());
            throw new RuntimeException(e);
        }finally {
            writer.flush();
            writer.close();
        }
    }
}
